package com.sx.micowell.module.collect

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.SPUtils
import com.gyf.immersionbar.ImmersionBar
import com.example.mvvm.bar.TitleBar
import com.miloyu.modelnet.response.LearnedCourseVO
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragMineCollectBinding
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.CollectModel
import com.sx.micowell.framework.model.LearnedCourseModel
import com.sx.micowell.framework.tools.AppDialogUtils
import com.sx.micowell.module.learned.LearnCourseListAdapter

/**
 * @Author: YY
 * @Time: 2021/8/10
 * @Describe 我的收藏
 */
@Route(path = RoutePath.MINE_COLLECT)
class MineCollectFragment : MyFragment<FragMineCollectBinding, ContainerActivity>(), TitleBarAction {

    lateinit var viewModel: CollectModel

    private var collectListAdapter: CollectListAdapter? = null

    override fun initViewModel() {
        super.initViewModel()
        viewModel = getFragmentScopeViewModel(CollectModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_mine_collect,BR.viewModel,viewModel)
    }

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    override fun initData() {
        setCollectList()
        viewModel.getCollectList()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.deleteEvent.observe(this, { aBoolean ->
            viewModel.getCollectList()
        })
    }

    override fun getTitleBar(): TitleBar? {
        return binding.title
    }

    override fun onLeftClick(v: View?) {
        myActivity.finish()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun setCollectList() {
        //取消recyclerview单独的滑动效果
        binding.recyclerCollect.isNestedScrollingEnabled = false
        //设置布局管理器
        binding.recyclerCollect.layoutManager = LinearLayoutManager(context)

        viewModel.collectListData.observe(this) {

            if (it != null && it.size > 0 ) {
                binding.layoutEmpty.visibility = View.GONE
                if (collectListAdapter == null) {
                    collectListAdapter = CollectListAdapter()
                    collectListAdapter!!.data.clear()
                    collectListAdapter!!.data = it
                    binding.recyclerCollect.adapter = collectListAdapter
                    collectListAdapter!!.addChildClickViewIds(R.id.collect,R.id.image_delete)
                    collectListAdapter!!.setOnItemChildClickListener { adapter, view, position ->
                        val collectData: LearnedCourseVO = adapter.getItem(position) as LearnedCourseVO
                        if(view.id==R.id.image_delete){
                            // 删除课程
                            AppDialogUtils.showBasicDialog(
                                context, "提示", "确定要取消收藏吗？",
                                object : AppDialogUtils.IOnClickListener {
                                    override fun leftListener() {}
                                    override fun rightListener() {
                                        viewModel.deleteCollectCourse(collectData.id.toString())
                                    }
                                })
                        }else{
                            //课程详情

                            viewModel.context.startContainerFragment(RouteManager.getInstance().getCourseVideoFragment(collectData.id.toString()))
                        }

                    }

                    collectListAdapter!!.setOnItemClickListener { adapter, view, position ->
                        val collectData: LearnedCourseVO = adapter.getItem(position) as LearnedCourseVO
                    }
                } else {
                    collectListAdapter!!.data.clear()
                    collectListAdapter!!.data = it
                    binding.recyclerCollect.adapter = collectListAdapter
                }

            } else {
                binding.recyclerCollect.adapter = null
                binding.layoutEmpty.visibility = View.VISIBLE
            }

        }
    }
}